package cn.xiaobai.j2exe.controller;

import cn.hutool.core.io.FileUtil;
import cn.xiaobai.j2exe.AbstractApplication;
import cn.xiaobai.j2exe.utils.FileUtils;
import cn.xiaobai.j2exe.utils.SourceUtils;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.TextFieldListCell;
import javafx.scene.image.ImageView;

import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.net.URL;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.ResourceBundle;

/**
 * jvm 选项
 *
 * @Author yangdaji
 * @Date 2023/12/26
 * @PackageName cn.xiaobai.j2exe.controller
 * @ClassName JvmSelectionController
 */
public class JvmSelectionController extends AbstractApplication {


    @FXML
    private TextField jre_input;

//    @FXML
//    private Button jre_select_bt;

    @FXML
    private Button jvm_args_add_bt;

    @FXML
    private Button jvm_args_del_bt;


    @FXML
    private ListView<String> jvm_setting_list;

    private ObservableList<String> argsList = FXCollections.observableArrayList();

//    @FXML
//    private AnchorPane jvm_select_pane;

    /**
     * swing 文件选择器,支持同时选择目录和文件
     */
    private static JFileChooser jFileChooser;


    private static String delItemValue;

    public void jre_input() {
        String jrePath = FileUtils.reversPath(jre_input.getText());
        globalConvertConfig.setJdk(jrePath != null ? (FileUtil.isNotEmpty(Paths.get(jrePath).toFile()) ? jrePath : null) : null);
    }

    @FXML
    public void on_select_jre_click() {
        int select = jFileChooser.showDialog(null, "select");
        if (select == JFileChooser.APPROVE_OPTION) {
            File selectedFile = jFileChooser.getSelectedFile();
            if (FileUtil.isNotEmpty(selectedFile)) {
                String jdkPath = FileUtils.reversPath(selectedFile);
                globalConvertConfig.setJdk(jdkPath);
                jre_input.setText(jdkPath);
                updateInitDir();
            } else {
                globalConvertConfig.setJdk(null);
            }
        }
    }

    @FXML
    public void on_jvm_args_add_click()  {
        argsList.add("双击修改此值!");
    }

    @FXML
    public void on_jvm_args_del_click() {
        if (delItemValue != null) {
            globalConvertConfig.getVmArgs()
                    .remove(delItemValue);
            argsList.remove(delItemValue);
        }
    }


    @Override
    protected void refresh() {
        jre_input.setText(globalConvertConfig.getJdk());
        // default args
        argsList.clear();
        // 删除存在的
        String defaultValue = "Xmx256M";
        globalConvertConfig.getVmArgs()
                .removeIf(defaultVal -> defaultVal.equals(defaultValue));
        argsList.add(defaultValue);
        argsList.addAll(globalConvertConfig.getVmArgs());
        globalConvertConfig.addVmArgs(argsList.toArray(new String[0]));
    }

    @Override
    protected void updateInitDir() {
        String jdkPath = globalConvertConfig.getJdk();
        if (jdkPath != null &&
                !Objects.equals(jdkPath, FileUtils.reversPath(jFileChooser.getCurrentDirectory()))) {
            jFileChooser.setCurrentDirectory(Paths.get(jdkPath).toFile());
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        addListener(this);
        initListener();
        initJFChooser();
        initIcon();

        refresh();
    }

    private void initListener() {
        jvm_setting_list.setItems(argsList);
        jvm_setting_list.setCellFactory(TextFieldListCell.forListView());
        jvm_setting_list.setEditable(true);
        jvm_setting_list.setOnEditCommit(event -> {
            String newValue = event.getNewValue();
            argsList.set(event.getIndex(), newValue);
            globalConvertConfig.addVmArgs(newValue);
        });

        jre_input.focusedProperty()
                .addListener((observable, oldValue, newValue) -> jre_input());

        // 注册选择监听器
        jvm_setting_list.getSelectionModel().selectedItemProperty()
                .addListener((observable, oldValue, newValue) -> {
                    delItemValue = newValue;
                });

    }

    /**
     * 初始化文件选择器
     */
    private void initJFChooser() {
        jFileChooser = new JFileChooser();
        jFileChooser.setDialogTitle("选择jre (支持 zip,tar.gz,目录)");
        jFileChooser.setAcceptAllFileFilterUsed(true);
        jFileChooser.setMultiSelectionEnabled(false);
        jFileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        jFileChooser.setFileSystemView(FileSystemView.getFileSystemView());
    }

    private void initIcon() {
        jvm_args_add_bt.setGraphic(new ImageView(SourceUtils.getPngImg("add-20.png")));
        jvm_args_del_bt.setGraphic(new ImageView(SourceUtils.getPngImg("del-20.png")));

    }


}
